/**
 * @Date: Mar 8, 2010 8:07:06 PM
 */
package com.philip.journal.home.controller.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;

import com.philip.journal.common.StringUtils;
import com.philip.journal.core.controller.ResponseHandler;
import com.philip.journal.core.exception.JournalException;
import com.philip.journal.core.service.ServiceProxy;
import com.philip.journal.home.controller.HomeFieldname;

/**
 * @author cry30
 */
public class SaveEntryAction extends AbstractEntryAction {

    /** Title form parameter. */
    private transient String title;

    /** Detail/Description form parameter. */
    private transient String detail;

    /** Entry Id form parameter. */
    private transient String entryId;

    /** Branch Id form parameter. */
    private transient String branchId;

    /**
     * Default delegate constructor.
     *
     * @param serviceProxy Service proxy to be injected by Spring.
     * @param responseHandler Response handler to be injected by Spring.
     */
    public SaveEntryAction(final ServiceProxy serviceProxy, final ResponseHandler responseHandler) {
        super(serviceProxy, responseHandler);
    }

    @Override
    public void execute() throws ServletException, IOException {

        try {
            final long localBranchId = parseId(this.branchId);
            long localEntryId = Long.parseLong("".equals(this.entryId) ? "0" : this.entryId);
            localEntryId = getServiceProxy().saveEntry(localBranchId, localEntryId, title, detail);
            final Map<String, Object> respObj = new HashMap<String, Object>();
            respObj.put(ENTRY_ID, localEntryId);
            respObj.put(HomeFieldname.TITLE, this.title);
            getResponseHandler().respondSuccess(getHttpServletResponse(), respObj);
        } catch (final JournalException e) {
            getLogger().debug(e.getMessage(), e);
            final String shortDesc = e.getMessage().substring(0, e.getMessage().indexOf(':'));
            final StringBuilder longDesc = new StringBuilder(e.getMessage().substring(
                    e.getMessage().indexOf(':') + 1));
            longDesc.append("\n" + getStringUtils().stackTraceToString(e));
            getResponseHandler().respondFail(getHttpServletResponse(), shortDesc, longDesc.toString());
        }
    }

    StringUtils getStringUtils() {
        return StringUtils.getInstance();
    }

    /**
     * Setter for {@link #title}. HTML tags are removed here.
     *
     * @param pTitle new title/name to set.
     */
    public void setTitle(final String pTitle) {
        this.title = getStringUtils().removeHtmlTags(pTitle);
    }

    public void setDetail(final String pDetail) {
        this.detail = pDetail;
    }

    public void setEntryId(final String pEntryId) {
        this.entryId = pEntryId;
    }

    public void setBranchId(final String pBranchId) {
        this.branchId = pBranchId;
    }
}
